package thread;

public class SyncDemo1 {
    public static boolean success1 = false;//表示1号线程是否取款成功
    public static boolean success2 = false;//表示2号线程是否取款成功

    public static void main(String[] args) {
        Bank bank = new Bank();
        int sum = 0;
        while(true){
            sum++;
            Thread t1 = new Thread(){
                @Override
                public void run() {
                    success1 = bank.getMoney(20000);
                }
            };

            Thread t2 = new Thread(){
                @Override
                public void run() {
                    success2 = bank.getMoney(20000);
                }
            };
            t1.start();
            t2.start();

            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(success1&&success2){//都是true说明两个人同时取款成功，出现了并发安全问题
                System.out.println("循环一共运行了"+sum+"次后, 出现并发问题, 两个都取到钱了");
                break;
            }else{
                bank.setAccount(20000);//重新将20000块钱存起来
                success1 = false;
                success2 = false;
            }
        }
    }
}
